//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace HappySprite
{
    public class RecycleBin
    {
        static readonly Type[] emptyType = new Type[] { };

        class RecycleBinEntry
        {
            public ConstructorInfo ConstructorInfo;
            public Queue<object> Pool = new Queue<object>();
        }

        Dictionary<Type, RecycleBinEntry> recycleBinEntryMap = new Dictionary<Type, RecycleBinEntry>();

        public object Create(Type type)
        {
            // Check arguments
            if (type == null) throw new ArgumentNullException("type");

            RecycleBinEntry objectEntry = GetRecycleBinEntry(type);

            if (objectEntry.Pool.Count == 0)
                return objectEntry.ConstructorInfo.Invoke(null);

            return objectEntry.Pool.Dequeue();
        }

        public T Create<T>() where T : new()
        {
            RecycleBinEntry objectEntry = GetRecycleBinEntry(typeof(T));

            if (objectEntry.Pool.Count == 0)
                return new T();

            return (T)objectEntry.Pool.Dequeue();
        }

        public void Recycle(object value)
        {
            // Check arguments
            if (value == null) throw new ArgumentNullException("value");

            RecycleBinEntry data = GetRecycleBinEntry(value.GetType());
            data.Pool.Enqueue(value);
        }

        RecycleBinEntry GetRecycleBinEntry(Type type)
        {
            RecycleBinEntry recycleBinEntry;

            if (!recycleBinEntryMap.TryGetValue(type, out recycleBinEntry))
            {
                recycleBinEntry = new RecycleBinEntry();
                recycleBinEntry.ConstructorInfo = GetConstrustorInfo(type);
                recycleBinEntryMap.Add(type, recycleBinEntry);
            }

            return recycleBinEntry;
        }

        static ConstructorInfo GetConstrustorInfo(Type type)
        {
            ConstructorInfo info = type.GetConstructor(emptyType);

            if (info == null)
                throw new InvalidOperationException("No default constructor for type.");

            return info;
        }
    }
}
